package com.ten.mall.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ten.mall.mapper.ItemMapper;
import com.ten.mall.mapper.ItemOrderMapper;
import com.ten.mall.service.ItemOrderService;
import com.ten.mall.utils.CommonUtis;
import com.ten.mall.vo.DianyuanOrderVo;
import com.ten.mall.vo.Item;
import com.ten.mall.vo.ItemOrder;
import com.ten.mall.vo.ItemOrderExample;
import com.ten.mall.vo.ItemOrderExample.Criteria;
import com.ten.mall.vo.OrderReturnVo;
import com.ten.mall.vo.OrderVo;
import com.ten.mall.vo.TimeVo;
import com.ten.mall.vo.UserAnalysis;
import com.ten.mall.vo.YaopinSellResult;
@Service
@Transactional
public class ItemOrderSericeImpl implements ItemOrderService {
	@Autowired
	private ItemOrderMapper itemOrderMapper;
	@Autowired
	private ItemMapper itemMapper;
	
	@Override
	@Transactional(readOnly=true)
	public Map<String,Object> selectByPages(Integer start, Integer size, OrderVo vo) {
		Map<String,Object> map =new HashMap<>();
		PageHelper.startPage(start, size);
		List<OrderReturnVo> list =itemOrderMapper.selectByDefind(vo);
		PageInfo<OrderReturnVo> info =new PageInfo<>(list);
		map.put("list", list);
		map.put("total", info.getTotal());
		return map;
	}

	@Override
	public String insertOrder(ItemOrder itemOrder) {
		String id =CommonUtis.creatOrderId();
		itemOrder.setId(id);
		itemOrder.setCreateTime(new Date());
		itemOrder.setUpdateTime(new Date());
		itemOrderMapper.insert(itemOrder);
		return id;
	}

	@Override
	@Transactional(readOnly=true)
	public ItemOrder selectById(String id) {
		ItemOrder itemOrder =itemOrderMapper.selectByPrimaryKey(id);
		return itemOrder;
	}

	@Override
	public int updateOrderState(String id, Integer state) {
		ItemOrder itemOrder =new ItemOrder();
		itemOrder.setOrderState(state);
		itemOrder.setId(id);
		int i =itemOrderMapper.updateByPrimaryKeySelective(itemOrder);
		return i;
	}

	@Override
	public int deleteOrder(String id) {
		// TODO Auto-generated method stub
		int i =itemOrderMapper.deleteByPrimaryKey(id);
		return i;
	}

	@Override
	public int updataOrder(ItemOrder itemOrder) {
		int i =itemOrderMapper.updateByPrimaryKeySelective(itemOrder);
		return i;
	}

	@Override
	public int creatOrder(ItemOrder itemOrder) {
		itemOrder.setCreateTime(new Date());
		itemOrder.setUpdateTime(new Date());
		int i =itemOrderMapper.updateByPrimaryKeySelective(itemOrder);
		//找出订单对应的产品id
		//更新产品库存
		return i;
	}

	@Override
	public int crateOrderUpdateItem(ItemOrder itemOrder) {
		// 查询出订单详情
		//更新产品库存
		Item item =itemMapper.selectByPrimaryKey(itemOrder.getGoodsId());
		if(item.getGoodsStorge()>=itemOrder.getOrderSum()){
			int j=item.getGoodsStorge()-itemOrder.getOrderSum();
			item.setGoodsStorge(j);
			itemMapper.updateByPrimaryKeySelective(item);
		}else {
			throw new RuntimeException("修改库存出错了!");
		}
		//保存订单信息
		itemOrder.setUpdateTime(new Date());
		int i =itemOrderMapper.updateByPrimaryKeySelective(itemOrder);
		return i;
	}
	/**
	 * 查询订单
	 */
	@Override
	@Transactional(readOnly=true)
	public List<ItemOrder> listItemOrder(String userId,
										 Integer start,Integer size) {
		PageHelper.startPage(start, size);
		ItemOrderExample example =new ItemOrderExample();
		Criteria c =example.createCriteria();
		c.andOrderOwnerEqualTo(userId);
		c.andOrderStateGreaterThanOrEqualTo(1);
		List<ItemOrder> list =itemOrderMapper.selectByExample(example);
		return list;
	}

	@Override
	@Transactional(readOnly=true)
	public List<ItemOrder> listItemOrderVo(OrderVo vo, Integer start, Integer size) {
		PageHelper.startPage(start, size);
		ItemOrderExample example =new ItemOrderExample();
		Criteria c =example.createCriteria();
		if(StringUtils.isNotBlank(vo.getDoctorId())){
			c.andOrderDoctorEqualTo(vo.getDoctorId());
		}
		if(vo.getState()==null){
			c.andOrderStateGreaterThan(0);
			//c.andOrderStateLessThan(8);
		}else{
			c.andOrderStateEqualTo(vo.getState());
		}
		if(StringUtils.isNotBlank(vo.getUserId())){
			c.andOrderOwnerEqualTo(vo.getUserId());
		}
		if(StringUtils.isNotBlank(vo.getYaoId())){
			c.andOrderYuliuEqualTo(vo.getYaoId());
		}
		List<ItemOrder> list =itemOrderMapper.selectByExample(example);
		return list;
	}

	@Override
	@Transactional(readOnly=true)
	public Map<String,Object> selectByDianyuan(Integer start, Integer size,TimeVo vo) {
		Map<String,Object> map =new HashMap<>();
		BigDecimal sum =new BigDecimal(0);
		PageHelper.startPage(start, size);
		List<DianyuanOrderVo> list =itemOrderMapper.selectByDianyuan(vo);
		PageInfo<DianyuanOrderVo> info =new PageInfo<>(list);
		for(DianyuanOrderVo d :list){
			sum=sum.add(d.getOrderTotal());
		}
		map.put("list", list);
		map.put("sum", sum);
		map.put("total", info.getTotal());
		return map;
	}
	
	@Override
	public int updateYaodianKucun(Map<String, Integer> map,ItemOrder itemOrder) {
		int i =itemOrderMapper.updateYaodianKucun(map);
		itemOrderMapper.updateByPrimaryKeySelective(itemOrder);
		return i;
	}
	/**
	 * 退款后返回库存
	 */
	@Override
	public int refuseOrder(ItemOrder itemOrder) {
		if(StringUtils.isNotBlank(itemOrder.getOrderYuliu())){
			itemOrderMapper.updateByPrimaryKeySelective(itemOrder);
			Map<String,Integer> map =new HashMap<>();
			map.put("yaoStorage", itemOrder.getOrderSum());
			map.put("goodsId", itemOrder.getGoodsId());
			map.put("yaoId", Integer.parseInt(itemOrder.getOrderYuliu()));
			itemOrderMapper.updateByRefuse(map);
		}else {
			Item item =itemMapper.selectByPrimaryKey(itemOrder.getGoodsId());
			Integer storage =item.getGoodsStorge()+itemOrder.getOrderSum();
			item.setGoodsStorge(storage);
			itemMapper.updateByPrimaryKeySelective(item);
		}
		int i =itemOrderMapper.updateByPrimaryKeySelective(itemOrder);
		return i;
	}

	@Override
	@Transactional(readOnly=true)
	public List<UserAnalysis> selectUserAnlaysizy(Integer start, Integer size, String myDate) {
		PageHelper.startPage(start, size);
		List<UserAnalysis> list =itemOrderMapper.selectUserAnlaysizy(myDate);
		return list;
	}
	/**
	 * 按产品统计
	 */
	@Override
	@Transactional(readOnly=true)
	public List<YaopinSellResult> selectSumGoods(Integer start, Integer size, String myDate) {
		PageHelper.startPage(start, size);
		List<YaopinSellResult> list =itemOrderMapper.selectSumGoods(myDate);
		return list;
	}

	@Override
	@Transactional(readOnly=true)
	public List<OrderReturnVo> selectByDefindId(OrderVo vo) {
		List<OrderReturnVo> order =itemOrderMapper.selectByDefind(vo);
		return order;
	}
	/**
	 * 通过医生分析
	 */
	@Override
	@Transactional(readOnly=true)
	public Map<String,Object> selectByDoctor(Integer start, Integer size, Map<String, String> map) {
		Map<String,Object> resultMap =new HashMap<>();
		PageHelper.startPage(start, size);
		List<OrderReturnVo> list =itemOrderMapper.selectByDoctor(map);
		PageInfo<OrderReturnVo> info =new PageInfo<>(list);
		resultMap.put("list", list);
		resultMap.put("total", info.getTotal());
		return resultMap;
	}

	@Override
	@Transactional(readOnly=true)
	public Map<String, Object> selectByYaodian(Integer start, Integer size, Map<String, String> map) {
		Map<String,Object> resultMap =new HashMap<>();
		PageHelper.startPage(start, size);
		List<OrderReturnVo> list =itemOrderMapper.selectByYaodian(map);
		PageInfo<OrderReturnVo> info =new PageInfo<>(list);
		resultMap.put("list", list);
		resultMap.put("total", info.getTotal());
		return resultMap;
	}

	@Override
	@Transactional(readOnly=true)
	public Map<String, Object> selectBySaleMan(Integer start, Integer size, Map<String, String> map) {
		Map<String,Object> resultMap =new HashMap<>();
		PageHelper.startPage(start, size);
		List<OrderReturnVo> list =itemOrderMapper.selectBySaleMan(map);
		PageInfo<OrderReturnVo> info =new PageInfo<>(list);
		resultMap.put("list", list);
		resultMap.put("total", info.getTotal());
		return resultMap;
	}

	@Override
	@Transactional(readOnly=true)
	public Map<String, Object> listItemTimeVo(Integer start,Integer size,TimeVo vo) throws Exception{
		Map<String,Object> map =new HashMap<>();
		PageHelper.startPage(start, size);
		ItemOrderExample example =new ItemOrderExample();
		Criteria c =example.createCriteria();
		if(StringUtils.isNotBlank(vo.getStartTime())&&StringUtils.isNotBlank(vo.getEndTime())){
			c.andUpdateTimeBetween(CommonUtis.toDate("yyyy-MM-dd hh:mm", vo.getStartTime()), CommonUtis.toDate("yyyy-MM-dd hh:mm", vo.getEndTime()));
		}
		if(StringUtils.isNotBlank(vo.getUserId())){
			c.andOrderOwnerEqualTo(vo.getUserId());
		}
		if(StringUtils.isNotBlank(vo.getDoctorId())){
			c.andOrderDoctorEqualTo(vo.getDoctorId());
		}
		if(StringUtils.isNotBlank(vo.getYaoId())){
			c.andOrderYuliuEqualTo(vo.getYaoId());
		}
		BigDecimal sum =new BigDecimal(0);
		List<ItemOrder> list =itemOrderMapper.selectByExample(example);
		PageInfo<ItemOrder> info =new PageInfo<>(list);
		for(ItemOrder order:list){
			sum =sum.add(order.getOrderTotal());
		}
		map.put("list", list);
		map.put("sum", sum);
		map.put("total", info.getTotal());
		return map;
	}

	@Override
	@Transactional(readOnly=true)
	public List<ItemOrder> selectByPrapare(String orderPrepare) {
		ItemOrderExample example =new ItemOrderExample();
		Criteria c =example.createCriteria();
		c.andOrderPrepareEqualTo(orderPrepare);
		List<ItemOrder> list =itemOrderMapper.selectByExample(example);
		return list;
	}
}
